package org.zjx.server;

import org.zjx.message.Message;
import org.zjx.message.NotificationMessage;

import java.util.Map;

public class PushService {
    private final SessionManager sessionManager;

    public PushService() {
        this.sessionManager = SessionManager.getInstance();
    }

    /**
     * 向指定客户端推送消息
     */
    public void pushToClient(String clientId, Message message) {
        ClientSession session = sessionManager.getSessionByClientId(clientId);
        if (session != null && session.isActive()) {
            session.sendMessage(message);
        }
    }

    /**
     * 向所有客户端广播消息
     */
    public void broadcast(Message message) {
        for (ClientSession session : sessionManager.getAllSessions().values()) {
            if (session.isActive()) {
                session.sendMessage(message);
            }
        }
    }

    /**
     * 向特定主题的客户端广播通知
     */
    public void broadcastNotification(String topic, NotificationMessage notification) {
        for (ClientSession session : sessionManager.getAllSessions().values()) {
            if (session.isActive()) {
                session.sendMessage(notification);
            }
        }
    }
}